/*
 * Copyright (C) 2010- Peer internet solutions
 * 
 * This file is part of mixare.
 * 
 * This program is free software: you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published by 
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version. 
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 * for more details. 
 * 
 * You should have received a copy of the GNU General Public License along with 
 * this program. If not, see <http://www.gnu.org/licenses/>
 */
package org.mixare;

import org.mixare.data.Json;
import org.mixare.gui.Globals;
import org.mixare.render.Matrix;
import org.mixare.render.MixVector;

import android.app.Activity;
import android.content.Intent;
import android.location.Location;
import android.util.Log;

public class MixState{
	public static int NOT_STARTED = 0; 
	public static int PROCESSING = 1; 
	public static int READY = 2; 
	public static int DONE = 3; 

	int nextLStatus = MixState.NOT_STARTED;
	String downloadId;
	DownloadResult dRes;

	public Location curFix;
	private float curBearing, curPitch;
	public float screenWidth, screenHeight;

	public String radius = "20";

	public String startUrl = "";

	public Json jLayer = new Json();
	boolean detailsView = false;
	
	String tt;
	
	public void onCatch(MixContext ctx, String line){
		ctx.mixView.experience = Globals.getJsonParameter(line, Globals.EXP);
		ctx.mixView.level = Globals.getJsonParameter(line, Globals.LEVEL);
		ctx.mixView.caughtId = tt;
		ctx.mixView.nextDialog = MixView.DIALOG_CATCH;
	}
	
	public void onBeenCaught(MixContext ctx, String id){
		ctx.mixView.catchId = id;
		ctx.mixView.nextDialog = MixView.DIALOG_CAUGHT;
	}
	
	public void onCatchFailed(MixContext ctx){
		ctx.mixView.nextDialog = MixView.DIALOG_CATCH_FAILED;
	}
	
	boolean handleEvent(MixContext ctx, Marker ma, String onPress) {
		float[] dist = new float[1];
		dist[0] = 0;		
		Log.d("MixState","handle event, " + curFix.getLatitude() + "," + curFix.getLongitude() + "," +  ctx.curLoc.getLatitude() + "," + ctx.curLoc.getLongitude());
		Location.distanceBetween(curFix.getLatitude(), curFix.getLongitude(), ma.mGeoLoc.getLatitude(), ma.mGeoLoc.getLongitude(), dist);
		if(dist[0] < Globals.CATCHING_DISTANCE){
			Log.d("MixState", ""+dist[0]);
			try {
				String response = Globals.sendPostRequest("req_id=CATCH&user="+ctx.mixView.id+"&caught="+ma.mText);
				Log.d("MixState", "request: req_id=CATCH&user="+ctx.mixView.id+"&caught="+ma.mText);
				Log.d("MixState", "response: "+response);
				int code = Integer.parseInt(response.substring(0, response.indexOf(";")));
				String line = response.substring(response.indexOf(";") + 1);
				if(code == 200){
					if (line.startsWith("catch fail")){
						onCatchFailed(ctx);
					}
					else {
						tt = ma.mText;
					}
				}
			} catch(Exception ex){
				Log.d("MixState", ex.toString());
			}
		}
		else if (onPress != null){
			if (onPress.startsWith("webpage")) {
				try {
					String webpage = MixUtils.parseAction(onPress);
					this.detailsView = true;
					ctx.loadWebPage(webpage);
				} catch (Exception ex) {
				}
			} else if (onPress.startsWith("return")){
				Intent intent = new Intent();
				intent.putExtra("RESULT", ma.mId);
				ctx.mixView.setResult(Activity.RESULT_OK, intent);
				ctx.mixView.finish();
			}
		} 
		return true;
	}

	public float getCurBearing() {
		return curBearing;
	}
	public float getCurPitch() {
		return curPitch;
	}

	public void calcPitchBearing(Matrix rotationM) {

		MixVector looking = new MixVector();
		rotationM.transpose();
		looking.set(1, 0, 0);
		looking.prod(rotationM);
		this.curBearing = (int) (MixUtils.getAngle(0, 0, looking.x, looking.z)  + 360 ) % 360 ;

		rotationM.transpose();
		looking.set(0, 1, 0);
		looking.prod(rotationM);
		this.curPitch = -MixUtils.getAngle(0, 0, looking.y, looking.z);
	}
}
